const Model = require('../models/hanziPinyinModel')

const getAllHanziPinyins = async (ctx) => {
  ctx.body = { success: true, data: await Model.getAllHanziPinyins() }
}

const getHanziPinyinById = async (ctx) => {
  const { id } = ctx.params
  const hanziPinyin = await Model.getHanziPinyinById(id)
  if (!hanziPinyin) ctx.throw(404, 'Hanzi Pinyin not found')
  ctx.body = hanziPinyin
}

const createHanziPinyin = async (ctx) => {
  const pinyinData = ctx.request.body
  const pinyinId = await Model.createHanziPinyin(pinyinData)
  ctx.status = 201
  ctx.body = { id: pinyinId, ...pinyinData }
}

const updateHanziPinyin = async (ctx) => {
  const { id } = ctx.params
  const pinyinData = ctx.request.body
  await Model.updateHanziPinyin(id, pinyinData)
  ctx.body = { success: true }
}

const deleteHanziPinyin = async (ctx) => {
  const { id } = ctx.params
  await Model.deleteHanziPinyin(id)
  ctx.status = 204
}

module.exports = {
  getAllHanziPinyins,
  getHanziPinyinById,
  createHanziPinyin,
  updateHanziPinyin,
  deleteHanziPinyin,
}